package com.shop.cereshop.agent.param.user;

import com.shop.cereshop.commons.config.RsaProperties;
import com.shop.cereshop.commons.utils.RsaUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotNull;

@Data
@Accessors(chain = true)
@ApiModel(value = "LoginDTO", description = "登录请求DTO")
@Slf4j
public class LoginDTO {
    @ApiModelProperty(value = "验证码")
    private String verificationCode;

    @ApiModelProperty(value = "手机号/uuid(需要加密)", required = true)
    @NotNull
    private String phone;


    @ApiModelProperty(value = "密码(需要加密)")
    private String password;

    public LoginDTO decrypt() throws Exception {
        LoginDTO res = this;
        try {
            if (this.phone != null) {
                res.setPhone(RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, this.phone));
            }
            if (this.password != null) {
                res.setPassword(RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, this.password));
            }
            if (this.verificationCode != null) {
                res.setVerificationCode(this.verificationCode);
            }
        } catch (Exception e) {
            log.error("解密失败");
        }
        return res;
    }
}
